package com.hsq.socket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

public class SocketTCP01Client {
    public static void main(String[] args) throws IOException {
        //1.连接服务端
        Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
        System.out.println("Client socket = " + socket.getClass());
        //得到socket对象关联的输出流对象
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("Hello server".getBytes());
        socket.shutdownOutput();
        InputStream inputStream = socket.getInputStream();
        byte[] buf = new byte[1024];
        int readLine = 0;
        while ((readLine = inputStream.read(buf)) != -1) {
            System.out.println(new String(buf, 0, readLine));
        }

        outputStream.close();
        inputStream.close();
        socket.close();
        System.out.println("Client 退出");
    }
}
